import http from '../../utils/request'
import md5 from '../../utils/md5.js';//支付页面引入md5.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeIndex:0,
        payNumber:{},
        paylist: [],
        xieyibox:false,
        userInfo:null,
        sessionInfo:'',
        intergal:'',
        isShow:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //判断是否登录
        let userInfo = JSON.parse(wx.getStorageSync('userInfo')) || null
        let opses = JSON.parse(wx.getStorageSync('third_Session')) || null
        this.setData({userInfo:userInfo,sessionInfo:opses})
        this.getMyIntegral1()
        this.interlist()
    },
    //实时获取积分
    async getMyIntegral1(){
        let that = this
        let options={
                url:'/getMyIntegral',
                method:'POST',
                data:{userId:JSON.parse(wx.getStorageSync('userInfo')).id},
                header:{"content-type":"application/json;charset=UTF-8"}
            }
        let res = await http(options)
        that.setData({intergal:res.data.obj})
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //显示协议
    xieyiishow(){
        this.setData({xieyibox:true})
    },
    //关闭协议
    xieyiBotom(){
        this.setData({xieyibox:false})
    },
    //金额切换
    uVip(e){
        let index = e.currentTarget.dataset.index
        let iteminfo = e.currentTarget.dataset.item
        this.setData({activeIndex:index,payNumber:iteminfo})
    },
    //提交
   async tijiaoPay(){
        let ordernum = new Date().getTime()+'_'+JSON.parse(wx.getStorageSync('userInfo')).id+'_'+this.data.payNumber.integral
        wx.setStorageSync('orderNumber', ordernum)
        let {userInfo} = this.data
        let that = this
        let options={
                url:'/WechatProgramPayForm',
                method:'POST',
                data:{
                    orderNumber: ordernum,
                    orderTitle: '积分充值',
                    orderAmount: that.data.payNumber.amount * 100,
                    openId:userInfo.openId
                },
                header:{"content-type":"application/json;charset=UTF-8"}
            }
        var res = await http(options)
        if(res.data.code == 0){
            var payInfo = res.data.obj.split(',')
            var newpayinf = []
            payInfo.map(item=>{
                newpayinf.push(item.replace('=',':'))
            })
            var timestamp = newpayinf[2].split(':')[1]
            var prepayid = newpayinf[3].replace(':','=').trim()
            var noncestr = newpayinf[6].split(':')[1]
            var stringA =`appId=wx91b2bce393fa3a10&nonceStr=${noncestr}&package=${prepayid}&signType=MD5&timeStamp=${timestamp}`
            var stringSignTemp=stringA+"&key=BBF522F0E6974DAE834977441495C11C" //注：key为商户平台设置的密钥key
            let signs=md5.hexMD5(stringSignTemp).toUpperCase() //注：MD5签名方式
            wx.requestPayment(
            {
                "timeStamp":timestamp,
                "nonceStr":noncestr,
                "package":prepayid,
                "signType":"MD5",
                "paySign":signs,
                "appId":"wx91b2bce393fa3a10",
                "success":function(res){
                    wx.showToast({
                        title: '微信支付成功',
                        icon: 'success',
                        duration: 2000,
                        success:function(){
                            setTimeout(function(){
                                wx.reLaunch({
                                    url:"/pages/mine/mine"
                                })
                            },2000);
                        }
                  })
                },
                "fail":function(res){
                    wx.showToast({
                        title: '微信支付失败',
                        icon: 'error',
                        duration: 2000
                  })
                }
            })
        }else{
            wx.showToast({
                title: res.data.msg,
                icon: 'error',
                duration: 2000
          })
        }
    },
    //获取积分充值list
    async interlist(){
        let that = this
        let options={
                url:'/getTopup',
                method:'POST',
                data:{userid:JSON.parse(wx.getStorageSync('userInfo')).id,authorization:wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).authorization:null},
                header:{"content-type":"application/json;charset=UTF-8"}
            }
        var res = await http(options)
        if(res.data.code==0){
            that.setData({paylist:res.data.obj,payNumber:res.data.obj[0]})
        }else{
            if(res.data.code==5){
                that.setData({isShow:true})
            }else{
                wx.showToast({
                    title: res.data.msg,
                    icon: 'error',
                    duration: 2000
                })
            }
        }
    }
})